package output;

import java.util.ArrayList;

import regexInput.DSTable;
import regexInput.Node;
import regexInput.StateNode;

public class TableDriver {
	private ArrayList<Node> charClass;
	private DSTable dfa;
	
	public TableDriver(ArrayList<Node> specs, DSTable table){
		charClass = specs;
		dfa = table;
	}
	
	public boolean testString(String sample){
		int state = 0;					//get start state
		int column = 0;
		for (int i = 0; i < sample.length(); i++) {			//run through string
			for (int k = 1; k < dfa.getCharClasses().size(); k++) {
				Node node = charClass.get(k - 1);
				for (int j = 0; j < node.getList().length; j++) {
					if (node.getList()[j] == sample.charAt(i)) {
						column = k-1;		//off by one when accessing the dfa table
						j = node.getList().length;
						k = dfa.getCharClasses().size();
					}
				}
			}
			state = Integer.parseInt(dfa.getState(state).getTransitions(column).get(0));
			if(state == -1){	//our dead state
				return false;
			}
		}
		if (dfa.getState(state).getState() == 1) {		//if(atFinalState)
			return true;
		}
		
		return false;
	}
}
